package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.AttrKeyService;
import com.atguigu.lease.admin.service.AttrValueService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.bean.AttrKey;
import com.atguigu.lease.model.bean.AttrValue;
import com.atguigu.lease.model.vo.AttrKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.atguigu.lease.common.result.Result.ok;

/**
 * @author LOGIN
 * @title AttrKeyController
 * @create 2025/6/24 0:34
 * @description
 */
@Tag(name="房间基本信息-属性和属性管理模块")
@RestController
@RequestMapping("/admin/attr")
public class AttrKeyController {
    @Autowired
    AttrKeyService attrKeyService;
    @Autowired
    AttrValueService attrValueService;

    @Operation(summary = "1、查询所有属性和属性值列表")
    @GetMapping("/list")
    public Result<List<AttrKeyVo>> list(){
        List<AttrKeyVo> attrKeyVos= attrKeyService.listAttrKeyVos();
        return ok(attrKeyVos);
    }
//    /admin/attr/key/saveOrUpdate
    @Operation(summary = "2、新增属性列表attrKey")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdate(
            @RequestBody AttrKey attrKey
    ){
        return Result.ok(attrKeyService.saveOrUpdate(attrKey));
    }
//    /admin/attr/value/saveOrUpdate
    @Operation(summary = "3、新增属性值attrValue")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdate(
            @RequestBody AttrValue attrValue
    ){
        return Result.ok(attrValueService.saveOrUpdate(attrValue));
    }
//    /admin/attr/value/deleteById?id=29
    @Operation(summary = "4、根据id删除attrValue")
    @DeleteMapping("/value/deleteById")
    public Result deleteValueById(
            @Parameter(in = ParameterIn.QUERY,required = true)
            @RequestParam Integer id
    ){
        return Result.ok(attrValueService.removeById(id));
    }

//    /admin/attr/key/deleteById?attrKeyId=11
    @Operation(summary = "5、根据attrKeyid删除attrkey")
    @DeleteMapping("/key/deleteById")
    public Result deleteKeyById(
            @RequestParam Integer attrKeyId
    ){
        //判断： 如果attKeyId没有可用的attrValue才可以删除
           Long count= attrValueService.count(Wrappers
                    .lambdaQuery(AttrValue.class)
                    .eq(AttrValue::getAttrKeyId,attrKeyId));
           if(count>0){
               return Result.error(ResultCodeEnum.DELETE_ERROR);
           }
           return Result.ok(attrKeyService.removeById(attrKeyId));
    }
}
